﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Star : System.Web.UI.Page
{
    MyDataDataContext db = new MyDataDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)//用户未登录，返回登录界面
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (!IsPostBack)//页面首次加载
            {
                MyDataDataContext db = new MyDataDataContext();
                //获取要显示的记录对象
                User user = (from r in db.User
                             where r.UserID == Session["UserId"].ToString()
                             select r).FirstOrDefault();
                //显示初始个人星级分数
                txtName.Text = Convert.ToString(user.Start);
            }

        }
    }
    protected void Btn_Click(object sender, EventArgs e)
    {

        MyDataDataContext db = new MyDataDataContext();

        //根据订单中的商品ID找出对应的卖家即要修改的记录对象
        var goods1 = (from g in db.SaleOfGoods
                    join r in db.Order
                    on g.GoodsID equals r.GoodsID
                    select g);

        var user = (from u in db.User
                     join s in goods1
                     on u.UserID equals s.UserID
                     select u).First();

        //检查输入评分是否符合规范
        if (Convert.ToDouble(txtName.Text) < 0 || Convert.ToDouble(txtName.Text) > 10)
            lblMsg.Text = "请检查输入的数字!";
        else
        {
            //获取新输入的评分，更新个人星级评分及评分次数
            user.Start = (Convert.ToDouble(txtName.Text) + (user.Start * user.StartNumber)) / (user.StartNumber + 1);
            user.StartNumber = user.StartNumber + 1;

            //提交
            db.SubmitChanges();
            lblMsg.Text = "评分成功！";

            Response.Redirect("Order2.aspx");
        }  
    }
}